#include <stdio.h>
#include "esp_task.h"
#include "deBug.h"
#include "Inf_es8311.h"
#include "Dri_Wifi.h"
#include "Dri_mqtt.h"
#include "Dri_Websocket.h"
#include "Inf_led.h"

uint8_t data[1024] = {0};
TaskHandle_t mainHandle;

void wifi_succ_callback(void)
{
    MY_LOGE("wifi succ");
    Dri_mqtt_Init();
    Dri_Websocket_Init();
}

void wifi_fail_callback(void)
{
    MY_LOGE("wifi fail");
}

/**
 * 每秒读取数据量： 16k(采样率) * 2byte(采样深度) =32k
 * 读取频率：32k / 1024byte(数据缓存) = 32Hz
 * 周期：1/32Hz = 31.25ms
 */
void app_main(void)
{
    mainHandle = xTaskGetCurrentTaskHandle();
    Inf_led_init();
    Inf_es8311_adio_init();
    Inf_ov2640_init();
    Dri_Wifi_Init(wifi_succ_callback, wifi_fail_callback);
    Dri_Wifi_Start();

    uint32_t cmd = AUDIO_VIDEO_NULL;
    while (1)
    {
        xTaskNotifyWait(0, 0, &cmd, portMAX_DELAY);
        if (cmd == AUDIO_ON_CLIENT2ESP)
        {
            MY_LOGE("开始发送我的声音");
            Dri_Websocket_audio_open(DIR_CLIENT2ESP);
        }
        else if (cmd == AUDIO_OFF_CLIENT2ESP)
        {
            MY_LOGE("停止发送我的声音");
            Dri_Websocket_audio_close(DIR_CLIENT2ESP);
        }
        else if (cmd == VIDEO_ON_ESP2CLIENT)
        {
            MY_LOGE("开始播放视频");
            Inf_led_on();
            Dri_Websocket_vedio_open();
        }
        else if (cmd == VIDEO_OFF_ESP2CLIENT)
        {
            MY_LOGE("停止播放视频");
            Inf_led_off();
            Dri_Websocket_vedio_close();
        }
        else if (cmd == AUDIO_ON_ESP2CLIENT)
        {
            MY_LOGE("开始接听来访者音频");
            Dri_Websocket_audio_open(DIR_ESP2CLIENT);
        }
        else if (cmd == AUDIO_OFF_ESP2CLIENT)
        {
            MY_LOGE("停止接听来访者音频");
            Dri_Websocket_audio_close(DIR_ESP2CLIENT);
        }
    }
}
